// for.cs
//
// Copyright 2010 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using NUglify.Helpers;
using NUglify.JavaScript.Visitors;

namespace NUglify.JavaScript.Syntax
{

    public sealed class ForStatement : IterationStatement
    {
        AstNode m_initializer;
        AstNode m_condition;
        AstNode m_incrementer;

        public AstNode Initializer
        {
            get => m_initializer;
            set => ReplaceNode(ref m_initializer, value);
        }

        public AstNode Condition
        {
            get => m_condition;
            set => ReplaceNode(ref m_condition, value);
        }

        public AstNode Incrementer
        {
            get => m_incrementer;
            set => ReplaceNode(ref m_incrementer, value);
        }

        /// <summary>Context for the first semicolon, separating the initializer and the condition</summary>
        public SourceContext Separator1Context { get; set; }

        /// <summary>Context for the second semicolon, separating the condition and the incrementor</summary>
        public SourceContext Separator2Context { get; set; }

        public BlockScope BlockScope { get; set; }
        
        public override SourceContext TerminatingContext
        {
            get
            {
                // if we have one, return it. If not, return what the body has (if any)
                return base.TerminatingContext ?? Body.IfNotNull(b => b.TerminatingContext);
            }
        }

        public override IEnumerable<AstNode> Children => EnumerateNonNullNodes(Initializer, Condition, Incrementer, Body);

        public ForStatement(SourceContext context)
            : base(context)
        {
        }

        public override void Accept(IVisitor visitor)
        {
            visitor?.Visit(this);
        }

        internal override bool EncloseBlock(EncloseBlockType type)
        {
            // pass the query on to the body
            return Body != null && Body.Count != 0 && Body.EncloseBlock(type);
        }


        public override bool ReplaceChild(AstNode oldNode, AstNode newNode)
        {
            if (Initializer == oldNode)
            {
                Initializer = newNode;
                return true;
            }

            if (Condition == oldNode)
            {
                Condition = newNode;
                return true;
            }

            if (Incrementer == oldNode)
            {
                Incrementer = newNode;
                return true;
            }

            if (Body == oldNode)
            {
                Body = ForceToBlock(newNode);
                return true;
            }

            return false;
        }
    }
}
